package com.sigmazero13.MKDummy.components;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.sigmazero13.MKDummy.R;
import com.sigmazero13.MKDummy.models.ManaColor;

public class CrystalDisplayIconView extends CrystalDisplayView {

	Map<ManaColor, Integer> crystals;
	Bitmap redImg;
	Bitmap blueImg;
	Bitmap greenImg;
	Bitmap whiteImg;
	View drawView;
	
	public CrystalDisplayIconView(Context c) {
		super(c);
		
		commonSetup(c);
	}

	public CrystalDisplayIconView(Context c, AttributeSet attrs) {
		super(c, attrs);
		
		commonSetup(c);
	}
	
	@Override
	public void onCrystalsChanged(Map<ManaColor, Integer> crystals) {
		this.crystals = crystals;
		drawView.invalidate();
	}
	
	private void commonSetup(Context c) {
		LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.player_data_draw, this, true);
		
		redImg = BitmapFactory.decodeResource(getResources(), R.drawable.crystal_red);
		blueImg = BitmapFactory.decodeResource(getResources(), R.drawable.crystal_blue);
		greenImg = BitmapFactory.decodeResource(getResources(), R.drawable.crystal_green);
		whiteImg = BitmapFactory.decodeResource(getResources(), R.drawable.crystal_white);
		
		drawView = new DrawView(c);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		drawView.setLayoutParams(lp);
		this.addView(drawView);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int w = MeasureSpec.getSize(widthMeasureSpec);
		int h = redImg.getHeight() + 10;
		drawView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(h, MeasureSpec.UNSPECIFIED));
		setMeasuredDimension(w,h);
	}
	
	
	private class DrawView extends View {
		private List<CrystalInfo> crystalOrder;
		
		public DrawView(Context c) {
			super(c);
			crystalOrder = new ArrayList<CrystalInfo>();
			
			crystalOrder.add(new CrystalInfo(ManaColor.RED, redImg));
			crystalOrder.add(new CrystalInfo(ManaColor.BLUE, blueImg));
			crystalOrder.add(new CrystalInfo(ManaColor.GREEN, greenImg));
			crystalOrder.add(new CrystalInfo(ManaColor.WHITE, whiteImg));
		}
		
		@Override
		public void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
			Log.d("here", "here");
			
			int totalCrystals = 0;
			for (CrystalInfo ci : crystalOrder) {
				totalCrystals += crystals.get(ci.getColor());
			}
			
			if (totalCrystals == 0) return;
			
			int crystalWidth = redImg.getWidth();
			float spacingMax = crystalWidth;
			float spacing = spacingMax;
			
			float fullWidth = totalCrystals * crystalWidth;
			
			float leftover = canvas.getWidth() - fullWidth;
			spacing = leftover/(totalCrystals-1);
			if (spacing > spacingMax) spacing = spacingMax;
			
			fullWidth = (totalCrystals * crystalWidth) + ((totalCrystals-1) * spacing);
			float left = (canvas.getWidth()-fullWidth)/2;
			
			int x = 0;
			for (CrystalInfo ci : crystalOrder) {
				Integer numColor = crystals.get(ci.getColor());
				for (int i = 0; i < numColor; i++) {
					canvas.drawBitmap(ci.getCrystal(), left+x, 5, null);
					x += crystalWidth + spacing;
				}
			}
				
		}
		
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			int w = MeasureSpec.getSize(widthMeasureSpec);
			int h = MeasureSpec.getSize(heightMeasureSpec);
			setMeasuredDimension(w,h);
		}
		
	}
	
	private class CrystalInfo {
		private ManaColor color;
		private Bitmap crystal;
		
		public CrystalInfo(ManaColor color, Bitmap crystal) {
			this.color = color;
			this.crystal = crystal;
		}
		
		public ManaColor getColor() {
			return color;
		}
		
		public Bitmap getCrystal() {
			return crystal;
		}
	}

}
